package com.purong.purong.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.purong.purong.Contant;
import com.purong.purong.R;
import com.purong.purong.adapter.UserHouseAdapter;
import com.purong.purong.bean.UserHouseBean;
import com.purong.purong.bean.UserHouseListBean;
import com.purong.purong.widget.ConfirmDialog;
import com.purong.purong.widget.LoadingView;
import com.yao.sdk.net.ResponseInfo;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zengwenjie on 2017/12/15.
 */
public class UserHouseActivity extends BaseActivity {
    private RecyclerView mRecyclerView;
    private UserHouseAdapter mAdapter;
    private List<UserHouseBean> itemList=new ArrayList<>();
    private LoadingView loadingView;
    @Override
    public int getRootViewId() {
        return R.layout.activity_user_house;
    }

    @Override
    protected void onResume() {
        super.onResume();
        getUserHouse();
    }

    @Override
    public void initView() {
        showBack();
        showTitle("房产信息");
        loadingView=(LoadingView)findViewById(R.id.loading_view);
        loadingView.setNoResultToast("暂无相关数据记录","");
        loadingView.setLoadListener(new LoadingView.OnLoadListener() {
            @Override
            public void onReload() {
                getUserHouse();
            }

            @Override
            public void onRefresh() {

            }
        });
        mRecyclerView=(RecyclerView)findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        mAdapter=new UserHouseAdapter(this);
        mAdapter.setData(itemList);
        mAdapter.setOnUserHouseListener(new UserHouseAdapter.OnUserHouseListener() {
            @Override
            public void onDelete(final int position) {
                ConfirmDialog dialog=new ConfirmDialog(UserHouseActivity.this, "确定删除此房产信息？", new ConfirmDialog.onConfirmClickListenr() {
                    @Override
                    public void onConfirm() {
                        deleteHouse(itemList.get(position).getUser_house_id());
                    }

                    @Override
                    public void onCancel() {

                    }
                });
                dialog.show();
            }

            @Override
            public void onEdit(int position) {
                Intent intent=new Intent(UserHouseActivity.this,UserHouseAddActivity.class);
                intent.putExtra("USER_HOUSE",itemList.get(position));
                startActivity(intent);

            }
        });
        mRecyclerView.setAdapter(mAdapter);
    }
    private void getUserHouse(){
        loadingView.loading();
        findViewById(R.id.add).setVisibility(View.GONE);
        Map<String, String> params = new LinkedHashMap<String, String>();
        if (!request(Contant.HOUSE_LIST,params,true)){
            loadingView.fail();
        }
    }
    private void deleteHouse(String id){
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("user_house_id",id);
        request(Contant.HOUSE_DEL,params,true,true);
    }
    @Override
    public void onResponse(ResponseInfo responseInfo) {
        if (hasDestroy())
            return;
        super.onResponse(responseInfo);
        if (responseInfo.url.equals(Contant.HOUSE_LIST)){
            if (responseInfo.isRequestOk){
                try{
                    UserHouseListBean bean= JSON.parseObject(responseInfo.content,UserHouseListBean.class);
                    if (bean!=null&&bean.getUser_house_list()!=null){
                        itemList.clear();
                        itemList.addAll(bean.getUser_house_list());
                        mAdapter.notifyDataSetChanged();
                        if (itemList.size()==0){
                            loadingView.noResult();
                        }else {
                            loadingView.finish();
                        }
                        findViewById(R.id.add).setVisibility(View.VISIBLE);
                    }
                }catch (Exception e){
                    loadingView.fail();
                }
            }else {
                loadingView.fail();
            }
        }else if (responseInfo.url.equals(Contant.HOUSE_DEL)){
            if (responseInfo.isRequestOk){
                String id=responseInfo.params.get("user_house_id");
                for (int i = 0; i <itemList.size() ; i++) {
                    if (itemList.get(i).getUser_house_id().equals(id)){
                        itemList.remove(i);
                        mAdapter.notifyDataSetChanged();
                        break;
                    }

                }
                if (itemList.size()==0){
                    loadingView.noResult();
                }
            }
        }
    }

    @Override
    public void initData() {

    }

//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        if (requestCode==1){
//            if (resultCode==RESULT_OK){
//                getUserHouse();
//            }
//        }
//    }

    public void onClick(View v){
        switch (v.getId()){
            case R.id.add:
                Intent intent=new Intent(this,UserHouseAddActivity.class);
                startActivity(intent);
                break;
        }
    }
}
